package com.byx.imclientutil.netty;

import com.byx.imclientutil.entity.MessageType;
import com.byx.imclientutil.protobuf.ChatMessageProtobuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelPipeline;

/**
 * HeartBeatHandler
 *
 * @author Superb
 * @date 2021/4/4 17:14
 */
public class HeartBeatRespHandler extends ChannelInboundHandlerAdapter {
    private NettyWebSocketClientImpl imService;

    public HeartBeatRespHandler(NettyWebSocketClientImpl imService) {
        this.imService = imService;
    }

    /**
     * Calls {@link ChannelHandlerContext#fireChannelRead(Object)} to forward
     * to the next {@link ChannelInboundHandler} in the {@link ChannelPipeline}.
     * <p>
     * Sub-classes may override this method to change behavior.
     *
     * @param ctx
     * @param msg
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ChatMessageProtobuf.Msg heartbeatMsg = (ChatMessageProtobuf.Msg) msg;
        if (heartbeatMsg == null) {
            return;
        }
        if (heartbeatMsg.getMessageType() == MessageType.HEART_BEAT_MSG){
            System.out.println("收到服务端心跳消息"+heartbeatMsg);
        }else {
            ctx.fireChannelRead(msg);
        }
    }
}
